package com.ipzoe.web.module.open;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ServiceException;
import com.google.common.collect.Maps;
import com.ipzoe.common.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

@Slf4j
@Service
public class SobotApiManager {

    private static final String URL_TOKEN = "https://www.sobot.com/api/get_token";
    private static final String URL_RECORD = "https://www.sobot.com/comm-open/api/call/5/record";
    private static final String companyid = "631cfa85b1aa495e965438a7e263b33a";
    private static final String appid = "f6b1629786744e38937adbd8c9760e26";
    private static final String appkey = "9578T45yYQ8j";
    private static final String serviceEmail = "jojoguo@danchina.com";

    public String getToken(){
        long createTime = System.currentTimeMillis();
        String sign = MD5.create().digestHex(appid + createTime + appkey);
        RestTemplate restTemplate = new RestTemplate();

        Map<String, String> paramsMap = Maps.newHashMap();
        paramsMap.put("appid", appid);
        paramsMap.put("create_time", String.valueOf(createTime));
        paramsMap.put("sign", sign);

        log.info("sobot-token-req:{}", JSON.toJSONString(paramsMap));
        String jsonResponse = restTemplate.getForObject(URL_TOKEN+"?appid={appid}&create_time={create_time}&sign={sign}", String.class, paramsMap);
        log.info("sobot-token-res:{}", jsonResponse);
        JSONObject jsonObject = JSON.parseObject(jsonResponse);
        if(!"000000".equals(jsonObject.getString("ret_code"))){
            throw new CustomException(400,"获取token失败");
        }

        return jsonObject.getJSONObject("item").getString("token");
    }

    public JSONArray getRecords(String startTime, String endTime, int callDirection, int page, int size){

        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.add("token", getToken());

        Map<String, Object> paramsMap = Maps.newHashMap();
        paramsMap.put("companyid", companyid);
        paramsMap.put("appid", appid);
        paramsMap.put("service_email", serviceEmail);
        paramsMap.put("start_time", startTime);
        paramsMap.put("end_time", endTime);
        paramsMap.put("call_direction", callDirection);
        paramsMap.put("page", page);
        paramsMap.put("size", size);

        HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(paramsMap), headers);
        log.info("sobot-record-req:{}", JSON.toJSONString(paramsMap));
        String jsonResponse = restTemplate.postForObject(URL_RECORD, httpEntity, String.class);
        log.info("sobot-record-res:{}", jsonResponse);
        JSONObject jsonObject = JSON.parseObject(jsonResponse);
        if(!"000000".equals(jsonObject.getString("ret_code"))){
            throw new CustomException(400,"查询通话记录接口返回错误");
        }

        return jsonObject.getJSONArray("items");
    }

}
